package com.xiaoma.thread.join;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author maj
 * @ClassName JoinTester01
 * @Description
 * @date 2020-10-17 18:15
 */
public class JoinTester01 implements Runnable {

  private String name;

  public JoinTester01(String name) {
    this.name = name;
  }

  public void run() {
    System.out.printf("%s begins: %s\n", name, new Date());
    try {
      TimeUnit.SECONDS.sleep(4);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.printf("%s has finished: %s\n", name, new Date());
  }

  public static void main(String[] args) {
    Thread thread1 = new Thread(new JoinTester01("One"));
    Thread thread2 = new Thread(new JoinTester01("Two"));
    thread1.start();
    thread2.start();

    try {
      thread1.join();
      thread2.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println("Main thread is finished");
  }

}
